package com.hup.utils.io.batch.function;

import com.hup.utils.commons.TextUtil;
import com.hup.utils.io.batch.BaseBatch;
import com.hup.utils.io.batch.SimpleBatchResp;
import com.hup.utils.io.batch.constant.WinServiceStartType;
import lombok.extern.log4j.Log4j2;

import java.util.ArrayList;

/**
 * 使用注册表设置服务启动类型
 *
 * @author hugan
 * @date 2020/9/23
 */
@Log4j2
public class RegServiceStartType implements BaseBatch<SimpleBatchResp> {

    public static final String TAG_WAA = "WaaSMedicSvc";

    private final String serviceTag;
    private final WinServiceStartType startType;

    public RegServiceStartType(String serviceTag, WinServiceStartType startType) {
        this.serviceTag = serviceTag;
        this.startType = startType;
    }

    @Override
    public String cmd() {
        return "REG add \"HKLM\\SYSTEM\\CurrentControlSet\\Services\\" + serviceTag
                + "\" /v \"Start\" /t REG_DWORD /d \"" + startType.regStartType + "\" /f";
    }

    @Override
    public String desc() {
        return "注册表设置[" + serviceTag + "]为[" + startType.desc + "]";
    }

    @Override
    public SimpleBatchResp parse(ArrayList<String> lstResp) {
        log.debug("resp.size={}", lstResp.size());
        log.debug("[{}]", TextUtil.getLineWrap(lstResp));

        if (lstResp.get(0).contains("错误")) {
            return SimpleBatchResp.fail(this, lstResp.get(0));
        }

        if (!lstResp.get(0).contains("成功")) {
            return SimpleBatchResp.fail(this, lstResp);
        }

        return SimpleBatchResp.success();
    }

}
